package com.fish.yssb.wave;

import android.util.Log;

import java.util.HashMap;

import static com.fish.yssb.wave.WaveConsts.isShowLog;


public class PassSubInfoJX {



    private static WaveInfo waveInfo;
    public static WaveInfo parseData(byte [] arg,WaveInfo info)
    {
        waveInfo = info;
        int num =0;

        int szlnum = (waveInfo.szlNum-1)/16;

         szlnum = (szlnum+1);

        int pre_num = 4+4+waveInfo.mnlNum*2+2*szlnum;
        byte[] bytes = new byte[pre_num];
        int count = 0;

        for(int i=0;i<waveInfo.mnlNum;i++)
        {
            waveInfo.mnlPassList.get((i+1)+"").mnlPassSubList = new HashMap<>();
        }
        while (true)
        {

//            num = num+pre_num;
            if(num<arg.length)
            {
                System.arraycopy(arg, num, bytes, 0, pre_num);
                num = num +pre_num;
                parseSubData(count,bytes);
                count++;

            }else{
                break;
            }
        }
        return  waveInfo;

    }

    private  static void parseSubData(int count ,byte []arg)
    {
        int num = bytesToInt(arg,0);
        int xNum = bytesToInt(arg,4);


        for(int i=0;i<waveInfo.mnlNum;i++)
        {
            float para_a = waveInfo.mnlPassList.get((i+1)+"").paraA;
            float para_b = waveInfo.mnlPassList.get((i+1)+"").paraB;
            PassInfoSubData subInfo = new PassInfoSubData();
            subInfo.no = num;
            subInfo.xNum = xNum;
            short yval = bytesToShort(arg,8+2*i);;
            float val = para_a*yval+ para_b;



            if("V".equalsIgnoreCase(waveInfo.mnlPassList.get((i+1)+"").unit))
            {
                if(val>264)
                {
                    val = 264;
                }

                if(val<-264)
                {
                    val = -264;
                }
            }
            subInfo.yNum = val;

            subInfo.type =0;
            waveInfo.mnlPassList.get((i+1)+"").mnlPassSubList.put(count,subInfo);
            Log.e("====PassInfoSubData","PassInfoSubData.no:"+subInfo.no
            +" PassInfoSubData.xNum:"+subInfo.xNum+" PassInfoSubData.yNum:"+subInfo.yNum);
        }

        short tmp =  bytesToShort(arg,8+2*waveInfo.mnlNum);
        String shortTmp = Short.toString(tmp);
        int ten = Integer.parseInt(shortTmp, 16);
        String two = Integer.toBinaryString(ten);
        if(two.length()<8)
        {
            int tmp_num = 8-two.length();
            for(int i=0;i<tmp_num;i++)
            {
                two = "0"+two;
            }
        }
        StringBuffer buf = new StringBuffer(two);
        buf = buf.reverse();
        String strtmp = new String(buf);
        if(waveInfo.szlNum<16) {
            for (int j = 0; j < waveInfo.szlNum; j++) {
                PassInfoSubData subInfo = new PassInfoSubData();
                subInfo.no = num;
                subInfo.xNum = xNum;
                subInfo.yNum = Integer.parseInt(strtmp.substring(j, j + 1));
                subInfo.type = 1;
                waveInfo.szlPassList.get((j + 1) + "").szlPassSubList.put(count, subInfo);

                Log.e("====szlPassSubList", "szlPassSubList.no:" + subInfo.no
                        + " szlPassSubList.xNum:" + subInfo.xNum + " szlPassSubLists.yNum:" + subInfo.yNum);
            }
        }


//        if(waveInfo.szlNum<16)
//        {
//
//            String str16= byteToHex(arg[8+waveInfo.mnlNum*2]);
//            String str16_1= byteToHex(arg[8+waveInfo.mnlNum*2+1]);
//
//            int ten = Integer.parseInt(str16, 16);
//            ten = 30;
//            String two = Integer.toBinaryString(ten);
////            two = "30";
//            if(two.length()<8)
//            {
//                int num_16 = 8-two.length();
//                for(int i=0;i<num_16;i++)
//                {
//                    two = "0"+two;
//                }
//            }
//            StringBuffer buf = new StringBuffer(two);
//            buf = buf.reverse();
//
//            ten = Integer.parseInt(str16_1, 16);
//             two = Integer.toBinaryString(ten);
//            if(two.length()<8)
//            {
//                int num_16 = 8-two.length();
//                for(int i=0;i<num_16;i++)
//                {
//                    two = "0"+two;
//                }
//            }
//            buf = new StringBuffer(two);
//            buf = buf.reverse();
//        }
//        for(int i=0;i<waveInfo.szlNum;i++)
//        {
//            PassInfoSubData subInfo = new PassInfoSubData();
//            subInfo.no = num;
//            subInfo.xNum = xNum;
//            subInfo.yNum = bytesToByte(arg,8+waveInfo.mnlNum*2+i);
//            subInfo.type =0;
//            Log.e("====PassInfoSubData","PassInfoSubData.no:"+subInfo.no
//                    +" PassInfoSubData.xNum:"+subInfo.xNum+" PassInfoSubData.yNum:"+subInfo.yNum);
//        }

    }

    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

    public static int bytesToInt(byte[] ary, int offset) {
        int value;
        value = (int) ((ary[offset]&0xFF)
                | ((ary[offset+1]<<8) & 0xFF00)
                | ((ary[offset+2]<<16)& 0xFF0000)
                | ((ary[offset+3]<<24) & 0xFF000000));
        return value;
    }

    public static short bytesToShort(byte[] ary, int offset) {
        short value;
        value = (short) ((ary[offset]&0xFF)
                | ((ary[offset+1]<<8) & 0xFF00));
        return value;
    }

    public static int bytesToByte(byte[] ary, int offset) {
        int value;
        value = (short) ((ary[offset]&0xFF));
        return value;
    }
}